import router from "../router";

const Public = {
  // 将ID、ParentID这种数据格式转换为树格式
  arrayToTree(data, id, pid) {      // 将ID、ParentID这种数据格式转换为树格式
    if (!data || !data.length) return [];
    let targetData = [];                    // 存储数据的容器(返回)
    let records = {};
    let itemLength = data.length;           // 数据集合的个数
    for (let i = 0; i < itemLength; i++) {
      let o = data[i];
      records[o[id]] = o;
    }
    for (let i = 0; i < itemLength; i++) {
      let currentData = data[i];

      // currentData.open = false;
      // if (sessionStorage.getItem('currentPid') === currentData.id) {
      //   currentData.open = true;
      // }

      let parentData = records[currentData[pid]];
      if (!parentData && currentData.is_menu === '1') {
        targetData.push(currentData);
        continue;
      }
      // parentData.open = false;
      if (currentData.is_menu === '1') {
        parentData.children = parentData.children || [];
        parentData.children.push(currentData);
      }
    }
    return targetData;
  },

  // 将ID、ParentID这种数据格式转换为树格式(显示组织架构按钮权限操作)
  arrayToTreeList(data, id, pid) {      // 将ID、ParentID这种数据格式转换为树格式
    if (!data || !data.length) return [];
    let targetData = [];                    // 存储数据的容器(返回)
    let records = {};
    let itemLength = data.length;           // 数据集合的个数
    for (let i = 0; i < itemLength; i++) {
      let o = data[i];
      records[o[id]] = o;

    }
    for (let i = 0; i < itemLength; i++) {
      let currentData = data[i];
      currentData.open = true;
      let parentData = records[currentData[pid]];
      if (!parentData) {
        targetData.push(currentData);
        continue;
      }
      // parentData.open = false;
      parentData.children = parentData.children || [];
      parentData.children.push(currentData);
    }
    return targetData;
  },

//  获取对应id的对象
  getIdArray(arr,pid){
    var new_arr = []
    $.each(arr,function (index, item) {
      if(item.pid===pid){
        new_arr.push(item)
      }
    })
    return new_arr;
  },
//  获取对应id的对象
  getSortArray(arr,sort){
    var new_arr = []
    $.each(arr,function (index, item) {
      if(item.sort==sort){
        new_arr.push(item)
      }
    })
    return new_arr;
  },
  // 获取当前url参数
  GetRequest() {
    let url = location.href; //获取url中"?"符后的字串
    let theRequest = new Object();
    if (url.indexOf("?") != -1) {
      let str = url.substr(url.indexOf('?')+1);
      let strs = str.split("&");
      for(let i = 0; i < strs.length; i ++) {
        theRequest[strs[i].split("=")[0]]=unescape(strs[i].split("=")[1]);
      }
    }
    return theRequest;
  },
  //获取当前时间格式化的字符串
  getCurrentTime(timeStr) {
    function g(t) { if(t<10){t = '0'+ t;}return t; }
    Date.prototype.toLocaleString = function() {
      return this.getFullYear() + "-" + g(this.getMonth() + 1) + "-" + g(this.getDate()) + " " + g(this.getHours()) + ":" + g(this.getMinutes()) + ":" + g(this.getSeconds());
    };
    var str = timeStr?timeStr:new Date().getTime();
    var time = new Date(str);
    time = time.toLocaleString();
    return time;
  },
  backa(){
    // this.$router.back(-1);
  },
  // 手机号验证
  isMobile: (str) => {
    return /^(13[0-9]|15[012356789]|18[0123456789]|17[0123456789]|14[57])[0-9]{8}$/.test(str);
    var regTelePhone = /^((0\d{2,3}-\d{7,8})|(1[3584790]\d{9}))$/;///^0[\d]{2,3}-[\d]{7,8}$/;
  },
  // 电话验证
  isTel: (str) => {
    return /^((0\d{2,3}-\d{7,8})|(1[3584790]\d{9}))$/.test(str);
  },
  // 验证url
  isUrl: (str) => {
    return /((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+(:[0-9]+)?|(?:ww‌​w.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?‌​(?:[\w]*))?)/.test(str);
  },
  // 密码验证
  pwd1: (password) => {
    return /^(\w){6,16}$/.test(password);
  },
  // 字母数字和符号
  pwd16: (password) => {
    return /^(\w){1,16}$/.test(password);
  },
  pwd50: (password) => {
    return /^(\w){1,50}$/.test(password);
  },
  // 字母数字和符号
  pwd30: (password) => {
    return /^(\w){1,30}$/.test(password);
  },
  // 字母数字和符号
  pwd32: (password) => {
    return /^(\w){1,32}$/.test(password);
  },
  // 字母数字和符号
  pwd100: (password) => {
    return /^(\w){1,100}$/.test(password);
  },
};

export {Public};
